﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.IO;

namespace FriendlyFire.UserControls
{
    public partial class Transfers : UserControl
    {
        private Helper myHelper;
        private FriendlyFire myParent;
        int index = 0;
        List<string> uploads = new List<string>();
        List<string> downloads = new List<string>();


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="c">reference to client</param>
        /// <param name="ffHelper">reference to friendly fire helper</param>
        public Transfers(ref Helper ffHelper, FriendlyFire parent)
        {
            InitializeComponent();
            myHelper = ffHelper;
            myParent = parent;
        }

        public string OutputText { set { MessageBox.Show(value); } }

        /// <summary>
        /// Custom paint event to draw gradient and enable double buffering to reduce flicker on resize
        /// </summary>
        /// <param name="sender">Object sending the event</param>
        /// <param name="e">any event arguments that need to be passed</param>
        private void Transfers_Paint(object sender, PaintEventArgs e)
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            myHelper.DrawDefaultBackGroundGradient(e.Graphics, ClientRectangle);
        }



        /// <summary>
        /// Button event for opening a stream on a hard coded port - test function
        /// </summary>
        /// <param name="sender">Object sending the event</param>
        /// <param name="e">any event arguments that need to be passed</param>
        private void button2_Click_1(object sender, EventArgs e)
        {
            //send stream on port 1234
            //client.SendData(Encoding.ASCII.GetBytes("1234"), 233);
        }

        private void Transfers_Load(object sender, EventArgs e)
        {
            UpdateGrid();
        }

        private delegate void GridUpdate();
        public void UpdateGrid()
        {
            CreateTransfersGrid();
            CreateUploadsGrid();
        }

        public void UpdateDownload(string download)
        {/*
            for(int i = 2; i < gTransfers.RowsCount; i++)
            {
                if (gTransfers[i, 0].Value.ToString() == download.FileName && gTransfers[i, 2].Value.ToString() == download.ClientInfo.ClientName)
                    gTransfers[i, 1].Value = download.Percentage;
            }
            //UpdateGrid();*/
        }

        public void UpdateUpload(string upload)
        {
            //for (int i = 2; i < gUploads.RowsCount; i++)
            //{
            //    if (gUploads[i, 0].Value.ToString() == upload.FileName && gUploads[i, 2].Value.ToString() == upload.ClientInfo.ClientName)
            //        gUploads[i, 1].Value = upload.Percentage;
            //}
            ////UpdateGrid();
        }

        private delegate void DownloadAdd(string upload);
        public void AddDownload(string download)
        {
            //if (gTransfers.InvokeRequired)
            //{
            //    gTransfers.Invoke(new DownloadAdd(AddDownload), download);
            //}
            //else
            //{
            //    downloads.Add(download);
            //    InsertRow(gTransfers, download.FileName, download.Percentage, download.ClientInfo.ClientName);
            //}
        }
        private delegate void UploadAdd(string upload);
        public void AddUpload(string upload)
        {
            //if (gUploads.InvokeRequired)
            //{
            //    gUploads.Invoke(new UploadAdd(AddUpload), upload);
            //}
            //else
            //{
            //    uploads.Add(upload);
            //    InsertRow(gUploads, upload.FileName, upload.Percentage, upload.ClientInfo.ClientName);
            //}
        }

        private void CreateTransfersGrid()
        {
            //if (gTransfers.InvokeRequired)
            //{
            //    gTransfers.Invoke(new GridUpdate(CreateTransfersGrid));
            //}
            //else
            //{
            //    gTransfers.Rows.Clear();
            //    CreateHeader(gTransfers, "Downloads");
            //    foreach (FileReceivedData f in downloads)
            //        InsertRow(gTransfers, f.FileName, f.Percentage, f.ClientInfo.ClientName);
            //    SetUpLower(gTransfers);
            //}
        }
        private void CreateUploadsGrid()
        {
            //if (gUploads.InvokeRequired)
            //{
            //    gUploads.Invoke(new GridUpdate(CreateUploadsGrid));
            //}
            //else
            //{
            //    gUploads.Rows.Clear();
            //    CreateHeader(gUploads, "Uploads");
            //    foreach (FileReceivedData f in uploads)
            //        InsertRow(gUploads, f.FileName, f.Percentage, f.ClientInfo.ClientName);
            //    SetUpLower(gUploads);
            //}
        }

        private void CreateHeader(SourceGrid.Grid g, string Header)
        {
            int maxColor = 150;
            Color c = Color.Black;
            Color themeB = myParent.ffHelper.CurrentGradientColor;
            if (themeB.B < maxColor && themeB.R < maxColor && themeB.G < maxColor)
            {
                c = Color.White;
            }

            g.ColumnsCount = 3;
            SourceGrid.Cells.Views.Cell categoryView = new SourceGrid.Cells.Views.Cell();
            categoryView.Background = new DevAge.Drawing.VisualElements.BackgroundLinearGradient(themeB, Color.White, 0);
            categoryView.ForeColor = c;
            categoryView.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
            categoryView.Border = DevAge.Drawing.RectangleBorder.NoBorder;
            categoryView.Font = new Font(Font, FontStyle.Bold);

            SourceGrid.Cells.Views.ColumnHeader headerView = new SourceGrid.Cells.Views.ColumnHeader();

            int row = g.RowsCount;
            g.Rows.Insert(row);
            g[row, 0] = new SourceGrid.Cells.Cell(Header);
            g[row, 0].View = categoryView;
            g[row, 0].ColumnSpan = gTransfers.ColumnsCount;

            row++;
            g.Rows.Insert(row);

            SourceGrid.Cells.ColumnHeader header1 = new SourceGrid.Cells.ColumnHeader("FileName");
            header1.View = headerView;
            header1.AutomaticSortEnabled = false;
            g[row, 0] = header1;

            SourceGrid.Cells.ColumnHeader header2 = new SourceGrid.Cells.ColumnHeader("Percentage");
            header2.View = headerView;
            header2.AutomaticSortEnabled = false;
            g[row, 1] = header2;

            SourceGrid.Cells.ColumnHeader header3 = new SourceGrid.Cells.ColumnHeader("From");
            header2.View = headerView;
            header2.AutomaticSortEnabled = false;
            g[row, 2] = header3;
        }

        public void InsertRow(SourceGrid.Grid g, string Filename, int percentage, string from)
        {
            int row = g.RowsCount;
            g.Rows.Insert(row);

            g[row, 0] = new SourceGrid.Cells.Cell(Filename);
            g[row, 0].View = new SourceGrid.Cells.Views.Cell();

            g[row, 1] = new SourceGrid.Cells.Link(percentage.ToString());
            g[row, 1].View = new SourceGrid.Cells.Views.Cell();

            g[row, 2] = new SourceGrid.Cells.Link(from);
            g[row, 2].View = new SourceGrid.Cells.Views.Cell();
        }

        private void SetUpLower(SourceGrid.Grid g)
        {
            g.Columns[0].AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize;
            g.Columns[1].AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize;
            g.Columns[2].AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize | SourceGrid.AutoSizeMode.EnableStretch;
            g.AutoStretchColumnsToFitWidth = true;
            g.AutoSizeCells();
            g.Columns.StretchToFit();
        }

    }
}
